﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data;
namespace LHWebApp.Json
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class JsonGetAlarmInfoList : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string op = context.Request.Params["op"];
            switch (op)
            {
                case "getStore":
                    GetAlarmList(context);
                    break;
                case "AlarmInfoDelBatch":
                    AlarmInfoDelBatch(context);
                    break;
                case "AlarmInfoConfirmBatch":
                    AlarmInfoConfirmBatch(context);
                    break;
                default:
                    context.Response.Write("{success: false}");
                    break;
            }
        }

        private void GetAlarmList(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string clientNo = context.Request.Params["clientNo"];
            string currentService = context.Request.Params["currentService"];
            if (string.IsNullOrEmpty(clientNo))
            {
                context.Response.Write("{success: false}");
            }
            else
            {
                try
                {
                    DataTable dt = LHWebApp.ServicesVehicleAlarmInfo.GetAlarmList(clientNo, currentService).Tables[0];
                    JSONHelper jsonHelp = new JSONHelper();
                    jsonHelp.success = true;
                    jsonHelp.totlalCount = dt.Rows.Count;
                    if (jsonHelp.totlalCount == 0)
                    {
                        context.Response.Write("{success: true}");
                    }
                    else
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            if (currentService == "5")
                            {
                                //jsonHelp.AddItem("VID", dr["VID"].ToString());
                                jsonHelp.AddItem("LICENSE_PLATE", dr["名称"].ToString());
                                //jsonHelp.AddItem("SIMCARD_NO", dr["SIMCARD_NO"].ToString());
                                jsonHelp.AddItem("ALARM_LOCATION", dr["报警位置"].ToString());
                                jsonHelp.AddItem("ALARM_TIME", dr["报警时间"].ToString() == "" ? "" : DateTime.Parse(dr["报警时间"].ToString()).ToString("yyyy-MM-dd hh:mm:ss"));
                                jsonHelp.AddItem("LONGITUDE", dr["经度"].ToString());
                                jsonHelp.AddItem("LATITUDE", dr["纬度"].ToString());
                                jsonHelp.AddItem("ALARM_TYPE", dr["报警类型"].ToString());
                                //jsonHelp.AddItem("ROWID", dr["ROWID"].ToString());
                                jsonHelp.ItemOk();
                            }
                            else
                            {
                                jsonHelp.AddItem("VID", dr["VID"].ToString());
                                jsonHelp.AddItem("LICENSE_PLATE", dr["LICENSE_PLATE"].ToString());
                                jsonHelp.AddItem("SIMCARD_NO", dr["SIMCARD_NO"].ToString());
                                jsonHelp.AddItem("ALARM_LOCATION", dr["ALARM_LOCATION"].ToString());
                                jsonHelp.AddItem("ALARM_TIME", dr["ALARM_TIME"].ToString() == "" ? "" : DateTime.Parse(dr["Alarm_Time"].ToString()).ToString("yyyy-MM-dd hh:mm:ss"));
                                jsonHelp.AddItem("LONGITUDE", dr["LONGITUDE"].ToString());
                                jsonHelp.AddItem("LATITUDE", dr["LATITUDE"].ToString());
                                jsonHelp.AddItem("ALARM_TYPE", dr["ALARM_TYPE"].ToString());
                                jsonHelp.AddItem("ROWID", dr["ROWID"].ToString());
                                jsonHelp.ItemOk();
                            }

                        }
                        context.Response.Write(jsonHelp.ToString());
                    }
                }
                catch (Exception)
                {
                    context.Response.Write("{success: false}");
                }
            }
        }
        private void AlarmInfoDelBatch(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string clientNo = context.Request.Params["clientNo"];
            string alarmIDList = context.Request.Params["alarmIDList"];
            if (string.IsNullOrEmpty(clientNo))
            {
                context.Response.Write("{success: false}");
            }
            else
            {
                try
                {
                    ServicesVehicleAlarmInfo.AlarmInfoDelBatch(alarmIDList);
                    context.Response.Write("{success: true}");
                }
                catch (Exception)
                {
                    context.Response.Write("{success: false}");
                }
            }
        }
        private void AlarmInfoConfirmBatch(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string clientNo = context.Request.Params["clientNo"];
            string alarmIDList = context.Request.Params["alarmIDList"];
            string operators = context.Request.Params["operators"];
            string disposition = context.Request.Params["disposition"];
            if (string.IsNullOrEmpty(clientNo))
            {
                context.Response.Write("{success: false}");
            }
            else
            {
                try
                {
                    ServicesVehicleAlarmInfo.AlarmInfoConfirmBatch(alarmIDList, operators, disposition);
                    context.Response.Write("{success: true}");
                }
                catch (Exception)
                {
                    context.Response.Write("{success: false}");
                }
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
